package cn.orange.ch03_linked;

import cn.orange.ListNode;

/**
 * LC876.链表的中间结点
 给你单链表的头结点 head ，请你找出并返回链表的中间结点。
 如果有两个中间结点，则返回第二个中间结点
 */
public class LC876 {
    public ListNode middleNode(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
    public static void main(String[] args) {
        LC876 alg = new LC876();
        ListNode h1 = new ListNode(1, new ListNode(2, new ListNode(6, new ListNode(4, new ListNode(5, new ListNode(6))))));
        System.out.println(alg.middleNode(h1));
        ListNode h2 = new ListNode(1, new ListNode(2, new ListNode(3)));
        System.out.println(alg.middleNode(h2));
    }
}
